package listeners;

import playlistData.PlaylistManager;
import android.app.Activity;
import android.content.DialogInterface;

import com.unipv14.androidgallery.CurrentImageActivity;
import com.unipv14.androidgallery.IListsRefresher;
import com.unipv14.androidgallery.ImageShower;

import fileManagers.FileUtilities;

public class PlayListListener implements IListener {

	private PlaylistManager manager;

	private Activity mainActivity;
	private IListsRefresher listRefresher;
	private RenamePlaylistListener renameListener;
	private String selectedFile;

	public PlayListListener(Activity mainActivity,
			IListsRefresher listRefresher, PlaylistManager manager) {
		this.mainActivity = mainActivity;
		this.manager = manager;
		this.listRefresher = listRefresher;
		renameListener = new RenamePlaylistListener(mainActivity, manager,
				listRefresher);
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		try {
			if (which == 1 && !manager.isPlaylistList()) {

				selectedFile = FileUtilities.nameToPath(
						getFolder(selectedFile), selectedFile);
				ImageShower.showImage(getListPath(), mainActivity,
						selectedFile,CurrentImageActivity.class);
				return;
			} else if (which == 0 && !manager.isPlaylistList()) {
				manager.removeImageFromPlaylist(selectedFile);
				listRefresher.refreshList();
			} else if (which == 0) {
				manager.removePlaylist(selectedFile);
				listRefresher.refreshList();

			} else {
				renameListener.buttonPressed(selectedFile);
			}
		}

		catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public String getFolder(String name) {
		return FileUtilities.getFolder(manager.getMap().getPathByName(name));
	}

	@Override
	public String[] getMenuOptions(String name) {
		if (manager.isPlaylistList()) {

			return new String[] { "Remove playlist", "Rename Playlist" };

		} else {
			return new String[] { "Remove from playlist", "Show Image" };
		}
	}

	@Override
	public void goToSubDirectory(String folder) {
		if (manager.isPlaylistList()) {
			manager.setPlaylistpath(manager.getMap().getPathByName(folder));
		}
	}

	@Override
	public void setSelectedFile(String selectedFile) {
		this.selectedFile = selectedFile;
	}

	@Override
	public String[] getListPath() {
		String[] listPath = manager.getMap().getPlaylist().keySet().toArray(new String[0]);
		return listPath;
	}

}